import 'package:enjoy_plus/models/user_info_model.dart';
import 'package:enjoy_plus/pages/repair/add_repair.dart';
import 'package:enjoy_plus/pages/repair/my_repair_list.dart';
import 'package:enjoy_plus/pages/house/add_house_from.dart';
import 'package:enjoy_plus/pages/house/building_list.dart';
import 'package:enjoy_plus/pages/house/house_detail.dart';
import 'package:enjoy_plus/pages/house/house_get_postion.dart';
import 'package:enjoy_plus/pages/house/house_list.dart';
import 'package:enjoy_plus/pages/house/room_list.dart';
import 'package:enjoy_plus/pages/login/login.dart';
import 'package:enjoy_plus/pages/mine/user_info.dart';
import 'package:enjoy_plus/pages/notice/noticeDetail.dart';
import 'package:enjoy_plus/providers/user_provider.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'pages/tab_bar_page.dart';
import 'package:provider/provider.dart';
import 'package:enjoy_plus/providers/kuan_provider.dart';
import 'package:enjoy_plus/providers/repair_provider.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

void main() {
  // 禁用鼠标追踪
  WidgetsFlutterBinding.ensureInitialized();
  debugDefaultTargetPlatformOverride = TargetPlatform.android;

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => UserProvider()),
        ChangeNotifierProvider(create: (_) => KuAnProvider()),
        ChangeNotifierProvider(create: (_) => RepairProvider()),
      ],
      child: MaterialApp(
        navigatorKey: navigatorKey,
        routes: {
          '/': (context) => const TabBarPage(),
          '/noticeDetail': (context) {
            final args = ModalRoute.of(context)!.settings.arguments as String;
            return NoticeDetail(id: args);
          },
          '/login': (context) => const Login(),
          '/houseList': (context) => const HouseList(),
          '/houseGetPostion': (context) => const HouseGetPostion(),
          '/buildingList': (context) {
            final args = ModalRoute.of(context)!.settings.arguments as String;
            return BuildingList(community: args);
          },
          '/roomList': (context) {
            final args = ModalRoute.of(context)!.settings.arguments as Map;
            return RoomList(
                community: args['community'] as String,
                building: args['building'] as String);
          },
          '/addHouseFrom': (context) {
            final args = ModalRoute.of(context)!.settings.arguments as Map;
            return AddHouseFrom(
              community: args['community'] as String?,
              building: args['building'] as String?,
              room: args['room'] as String?,
              id: args['id'] as String?,
              isEdit: args['isEdit'] as bool?,
            );
          },
          '/houseDetail': (context) {
            final args = ModalRoute.of(context)!.settings.arguments as String;
            return HouseDetail(id: args);
          },
          //报修相关
          '/myRepairList': (context) => const MyRepairList(),
          '/addRepair': (context) => const AddRepair(),
        },
        onGenerateRoute: (settings) {
          final tokenManager = TokenManager();
          final token = tokenManager.getToken() ?? '';
          if (token.isEmpty && settings.name != '/login') {
            return MaterialPageRoute(builder: (context) => const Login());
          }
          if (settings.name == '/userInfo') {
            return MaterialPageRoute(
              builder: (context) => UserInfo(
                userInfo: UserInfoModel(id: '', avatar: '', nickName: ''),
              ),
            );
          }
          return null;
        },
        initialRoute: '/',
      ),
    ),
  );
}
